package com.test.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String prefixPath = req.getContextPath(); //前缀
        if(username.equals("mnmnwq") && password.equals("123456")){
            //登陆成功
            req.getSession().setAttribute("username",username);
            resp.sendRedirect(prefixPath + "/index.jsp");
            System.out.println("ok");
        }else{
            //登陆失败
            resp.sendRedirect(prefixPath + "/login.jsp");
            System.out.println("no");
        }
    }
}
